//
//  SettingViewController.m
//  NewsReader
//
//  Created by blacksun on 10-12-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import "NewsReaderAppDelegate.h"
#import "DisclaimerViewController.h"
#import "SinaWeibo.h"
#import "TencentWeibo.h"
#import "WeiboOAuthController.h"


@implementation SettingViewController
@synthesize settingTitle,settingView;
@synthesize lableList;
@synthesize user;
@synthesize aboutTitles;
@synthesize aboutValues;






// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//NSString *labelText1 = @"刷新记录条数";
	NSString *labelText2 = @"保存记录天数";
	NSString *labelText3 = @"加载图片";
	NSString *labelText4 = @"内容优化";
	self.settingTitle = [NSArray arrayWithObjects:labelText2,labelText3,labelText4,nil];
	//[labelText1 release];
	[labelText2 release];
	[labelText3 release];
	[labelText4 release];
	
	CGRect switchFrame = CGRectMake(150.0, 10.0, 90.0, 40.0);
	CGRect sliderFrame = CGRectMake(150.0, 5.0, 90.0, 40.0);
	CGRect labelFrame = CGRectMake(250.0, 14.0, 20.0, 22.0);
	UIFont *font = [UIFont systemFontOfSize:12.0];
	
	/*UILabel *label1 = [[UILabel alloc] initWithFrame:labelFrame];
	label1.font = font;
	label1.text = [NSString stringWithFormat:@"%d",self.user.feedSizePerRequest];
	*/UILabel *label2 = [[UILabel alloc] initWithFrame:labelFrame];
	label2.font = font;
	label2.text = [NSString stringWithFormat:@"%d",self.user.saveFeedDays];
	self.lableList = [NSArray arrayWithObjects:label2,nil];
	//[label1 release];
	[label2 release];
	
	/*UISlider *slider1 = [[UISlider alloc] initWithFrame:sliderFrame];
	slider1.minimumValue = 10;
	slider1.maximumValue = 50;
	slider1.value = self.user.feedSizePerRequest;
	[slider1 addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
	*/UISlider *slider2 = [[UISlider alloc] initWithFrame:sliderFrame];
	slider2.minimumValue = 1;
	slider2.maximumValue = 10;
	slider2.value = self.user.saveFeedDays;
	[slider2 addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
	UISwitch *switch3 = [[UISwitch alloc] initWithFrame:switchFrame];
	switch3.on = self.user.requestImage;
	[switch3 addTarget:self action:@selector(didClickRequestImage:) forControlEvents:UIControlEventValueChanged];
	UISwitch *switch4 = [[UISwitch alloc] initWithFrame:switchFrame];
	switch4.on = self.user.optimizeContent;
	[switch4 addTarget:self action:@selector(didClickRequestImage:) forControlEvents:UIControlEventValueChanged];
	
	
	self.settingView = [NSArray arrayWithObjects:slider2,switch3,switch4,nil];
	//[slider1 release];
	[slider2 release];
	[switch3 release];
	[switch4 release];
	
	//开始构建 关于 部分的数据
	self.aboutTitles = [NSArray arrayWithObjects:@"版本",@"联系方式",@"免责声明",nil];
	self.aboutValues = [NSArray arrayWithObjects:@"0.8",@"ipoint.mobile@hotmail.com",@"",nil];
	
	
}
-(IBAction)goBack:(id)sender{
	[self.navigationController popViewControllerAnimated:YES];
}
-(void)sliderValueChange:(id)slider{
	UISlider *temp = (UISlider *)slider;
	NSInteger row = [self.settingView indexOfObject:temp];
	UILabel *resultLabe = (UILabel *)[self.lableList objectAtIndex:row];
	resultLabe.text =  [NSString stringWithFormat:@"%d",(int)temp.value];// temp.value;
	/* if (row ==0) {
		self.user.feedSizePerRequest = (int)temp.value;
	}else {*/
		self.user.saveFeedDays = (int)temp.value;
	//}

}
-(void)didClickRequestImage:(id)theSwitch{
	UISwitch *temp = (UISwitch *)theSwitch;
	NSInteger row = [self.settingView indexOfObject:temp];
	if (row == 1) {
		self.user.requestImage = temp.on;
	}else if (row ==2) {
		self.user.optimizeContent = temp.on;
	}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	if (section==0) {
		return [self.settingTitle count]; 
	}else if (section==1) {
		return 2;
	}else {
		return 3;
	}

}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	if (section==0) {
		return @"浏览设置"; 
	}else if (section==1) {
		return @"分享设置";
	}else {
		return @"关于";
	}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *SettingCellIdentifier = @"SettingCellIdentifier";
    
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SettingCellIdentifier] autorelease];
	}
	
	NSInteger row = [indexPath row];
	if ([indexPath section]==0) {
		cell.textLabel.text = [self.settingTitle objectAtIndex:row];
		[cell.contentView addSubview:[self.settingView objectAtIndex:row]];
		if (row<1) {
			[cell.contentView addSubview:[self.lableList objectAtIndex:row]];
		}

	}else if ([indexPath section]==1) {
		NSString *text = nil;
		if (row==0) {
			text = @"新浪微博";
		}else {
			text = @"腾讯微博";
		}
		cell.textLabel.text = text;

	}else {
		cell.textLabel.text = [self.aboutTitles objectAtIndex:row];
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90, 10.0, 180.0, 30.0)];
		label.textAlignment = UITextAlignmentRight;
		label.font = [UIFont systemFontOfSize:13];
		label.text = [self.aboutValues objectAtIndex:row];
		[cell.contentView addSubview:label];
		[label release];
		if (row == [self.aboutTitles count]-1) {
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		}
	}

	return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	if (indexPath.section==1) {
        BOOL hadAuthorization = NO;//是否已经认证过
        currentIndentify = indexPath.row;
        BaseWeibo *weibo = nil;
        NSString *message = nil;
        switch (indexPath.row) {
            case kSina:{
                weibo = [[SinaWeibo alloc] init];
                break;
            }
            case kQQ:
                weibo = [[TencentWeibo alloc] init];
                break;
            default:
                break;
        }
        
        if (weibo.oauthKey.tokenKey) {
            hadAuthorization = YES;
            NSString *weiboName = nil;
            if (currentIndentify == kSina) {
                weiboName = @"新浪";
            }else if(currentIndentify == kQQ){
                weiboName = @"腾讯";
            }
            message = [[NSString alloc] initWithFormat:@"您已绑定 %@ 微博用户:%@，是否要重新绑定?",weiboName,weibo.userName];
        }
        
        if (hadAuthorization) {
            UIAlertView *usageAlertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是",nil];
            [usageAlertView show];
        }else{
            [self gotoAuthorizeView:currentIndentify];
        }
        
        [message release];
        [weibo release];
        
	}else if (indexPath.section == 2) {
		if (indexPath.row == [self.aboutTitles count]-1) {
			DisclaimerViewController *controller = [[DisclaimerViewController alloc] initWithNibName:@"DisclaimerController" bundle:nil];
			[self.navigationController pushViewController:controller animated:YES];
			[controller release];
		}
	}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self gotoAuthorizeView:currentIndentify];
    }
}

- (void) gotoAuthorizeView:(NSInteger)identify{

    WeiboOAuthController *oauthController = [[WeiboOAuthController alloc] init];
    BaseWeibo *weibo = nil;
    switch (identify) {
        case kSina:{
            weibo = [[SinaWeibo alloc] init];
            weibo.oauthKey.tokenKey = nil;
            weibo.oauthKey.tokenSecret = nil;
            break;
        }
        case kQQ:
            weibo = [[TencentWeibo alloc] init];
            weibo.oauthKey.tokenKey = nil;
            weibo.oauthKey.tokenSecret = nil;
            weibo.oauthKey.callbackUrl = @"http://www.qq.com";
            break;
        default:
            break;
    }
    oauthController.weibo = weibo;
    oauthController.parentContrpller = nil;
    NewsReaderAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.navigationController pushViewController:oauthController animated:YES];
    
    [weibo release];
    [oauthController release];

}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
-(void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (void)dealloc {
	[self.lableList release];
	[self.settingTitle release];
	[self.settingView release];
	[self.user release];
	[self.aboutTitles release];
	[self.aboutValues release];
    [super dealloc];
}


@end
